package com.letme.web.controller.store;

import java.util.ArrayList;
import java.util.List;

import com.letme.common.core.domain.TreeSelect;
import com.letme.common.core.domain.entity.MallClassification;
import com.letme.store.service.IMallClassificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letme.common.annotation.Log;
import com.letme.common.core.controller.BaseController;
import com.letme.common.core.domain.AjaxResult;
import com.letme.common.enums.BusinessType;
import com.letme.common.utils.poi.ExcelUtil;

/**
 * 产品分类Controller
 *
 * @author lock
 * @date 2024-12-17
 */
@RestController
@Tag(name = "产品分类")
@RequestMapping("/store/classification")
public class MallClassificationController extends BaseController
{
    @Autowired
    private IMallClassificationService classificationService;

    /**
     * 查询产品分类列表
     */
    @Operation(summary ="查询产品分类列表")
    @PreAuthorize("@ss.hasPermi('store:classification:list')")
    @GetMapping("/list")
    public AjaxResult list(MallClassification mallClassification)
    {
        List<MallClassification> list = classificationService.selectMallClassificationList(mallClassification);
        return success(list);
    }

    @Operation(summary ="添加分类时查询父级分类")
    @PreAuthorize("@ss.hasPermi('store:classification:list')")
    @GetMapping("/queryTypeList")
    public AjaxResult queryTypeList()
    {
        MallClassification type = new MallClassification();
        type.setParentId(0L);
        List<MallClassification> list = new ArrayList<>();
        //添加顶级分类
        list.add(new MallClassification(0L,"父级分类"));
        list.addAll(classificationService.selectMallClassificationList(type));
        return success(list);
    }

    /**
     * 添加产品时查询产品分类列表
     */
    @Operation(summary ="添加产品时查询产品分类列表")
    @PreAuthorize("@ss.hasPermi('store:classification:queryTypeTree')")
    @GetMapping("/queryTypeTree")
    public AjaxResult queryTypeTree()
    {
        List<TreeSelect> list = classificationService.selectMallClassificationTreeList(new MallClassification());
        return success(list);
    }

    /**
     * 导出产品分类列表
     */
    @Operation(summary ="导出产品分类列表")
    @PreAuthorize("@ss.hasPermi('store:classification:export')")
    @Log(title = "产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallClassification mallClassification)
    {
        List<MallClassification> list = classificationService.selectMallClassificationList(mallClassification);
        ExcelUtil<MallClassification> util = new ExcelUtil<>(MallClassification.class);
        util.exportExcel(response, list, "产品分类数据");
    }

    /**
     * 获取产品分类详细信息
     */
    @Operation(summary ="获取产品分类详细信息")
    @PreAuthorize("@ss.hasPermi('store:classification:query')")
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId)
    {
        return success(classificationService.selectMallClassificationByClassId(classId));
    }

    /**
     * 新增产品分类
     */
    @Operation(summary ="新增产品分类")
    @PreAuthorize("@ss.hasPermi('store:classification:add')")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallClassification mallClassification)
    {
        return toAjax(classificationService.insertMallClassification(mallClassification));
    }

    /**
     * 修改产品分类
     */
    @Operation(summary ="修改产品分类")
    @PreAuthorize("@ss.hasPermi('store:classification:edit')")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallClassification mallClassification)
    {
        return toAjax(classificationService.updateMallClassification(mallClassification));
    }

    /**
     * 删除产品分类
     * 逻辑删除，删除后不显示，但还存在数据库中
     * @param classIds
     * @return
     */
    @Operation(summary ="批量删除产品分类--逻辑删除")
    @PreAuthorize("@ss.hasPermi('store:classification:remove')")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classIds}")
    public AjaxResult remove(@PathVariable Long[] classIds)
    {
        return toAjax(classificationService.fakeDelMallClassificationByClassIds(classIds));
    }

    /**
     * 删除产品分类
     * 真删除，删除后数据库不存在
     * @param classIds
     * @return
     */
    @Operation(summary ="批量删除产品分类--真删除")
    @PreAuthorize("@ss.hasPermi('store:classification:realRemove')")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/realDel/{classIds}")
    public AjaxResult realRemove(@PathVariable Long[] classIds)
    {
        return toAjax(classificationService.realDelMallClassificationByClassIds(classIds));
    }
}

